/*
 * @Descripttion:
 * @version:
 * @Author: mmzhang
 * @Date: 2021-07-27 16:02:34
 * @LastEditTime: 2022-03-31 14:55:10
 */
let vm = null
import clueCustomerMgr from '@/biz/customer/clueCustomer.js'
import poptipContent from '../components/poptipContent'
import CustomerMgr from '@/biz/customer/customerOpt.js'
import dt from '@dt/dt'
import table from '@/components/dt/table/render.js'
import nameOpt from '@/biz/option/name.js'
import channelMgr from '@/biz/channel/channel'

function columnField(_this) {
  let columns2 = {}
  columns2 = {
    customerName: {
      field: 'customerName',
      title: '客户姓名',
      minWidth: 120,
      fixed: 'left',
      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入客户姓名',
          input: {}
        }
      },
      slots: {
        default: (params, h) => {
          return [
            h('div', {}, [
              h('Icon', {
                props: {
                  type: params.row.hasAttention == 2 ? 'ios-star-outline' : 'ios-star',
                  size: '20'
                },
                style: {
                  marginRight: '6px',
                  color: '#ff9900',
                  cursor: 'pointer'
                },
                on: {
                  click: () => {
                    vm.getChangeAttention(params.row)
                  }
                }
              }),
              h('span', params.row.customerName)
            ])
          ]
        }
      }
    },
    plan: {
      title: '自获客打法',
      field: 'plan',
      minWidth: 160,
      align: 'center',
      header: {
        filter: {
          title: '自获客打法',
          key: 'planIds',
          placeholder: '请选择',

          load: (o) => {
            let params = {}
            channelMgr.planChannelChannelList(params).then((res) => {
              console.log(res, 'fdfffffffffffff')
              res.forEach((item) => {
                item.key = item.planId
                item.value = item.planName
              })
              o.select.options = res
            })
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },

    marketingTool: {
      title: '营销工具',
      field: 'marketingTool',
      minWidth: 160,
      align: 'center',
      header: {
        filter: {
          title: '营销工具',
          key: 'marketingTools',
          placeholder: '请选择',
          select: {
            multiple: true,
            options: dt.dictInfo().marketingTool
          }
        }
      }
    },
    authLocation: {
      title: '线索授权位置',
      field: 'authLocation',
      minWidth: 160,
      align: 'center',
      header: {
        filter: {
          title: '线索授权位置',
          key: 'authLocations',
          placeholder: '请选择',
          select: {
            multiple: true,
            options: dt.dictInfo().authLocation
          }
        }
      }
    },
    marketingActivity: {
      field: 'marketingActivity',
      title: '营销活动',
      minWidth: 200,
      header: {
        filter: {
          title: '营销活动',
          key: 'marketingActivity',
          placeholder: '请输入营销活动',
          input: {}
        }
      }
    },
    customerPhone: {
      field: 'customerPhone',
      minWidth: 130,
      fixed: 'left',
      header: {
        filter: {
          title: '手机号码',
          key: 'customerPhone',
          placeholder: '请输入手机号码',
          input: {}
        }
      }
    },
    recommender: {
      field: 'recommender',
      minWidth: 180,
      header: {
        filter: nameOpt.createPushPeople(
          {
            key: 'recommender',
            title: '推荐人',
            to: (info) => {
              return {
                recommender: info.workNo
              }
            }
          },
          'onCase'
        )
      },
      slots: {
        default: (params, h) => {
          let recommender = []
          if (params.row.recommender) {
            recommender = params.row.recommender.split(',')
          }
          return table.cell.text(h, {
            value:
              recommender && recommender.length > 0
                ? recommender.length > 1
                  ? `${recommender[0]}等${recommender.length}个`
                  : recommender[0]
                : '-'
          })
        }
      }
    },

    attributionConsultant: {
      field: 'attributionConsultant',
      minWidth: 120,
      fixed: 'left',
      header: {
        filter: {
          title: '归属顾问',
          key: 'userIds',
          placeholder: '请选择归属顾问',
          load: (o) => {
            o.select.options = clueCustomerMgr.selectTeamUser()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true,
            remote: (key) => {
              return clueCustomerMgr.selectTeamUser(key)
            }
          }
        }
      }
    },

    status: {
      field: 'status',
      minWidth: 120,
      header: {
        filter: {
          title: '客户进度',
          key: 'status',
          placeholder: '请选择客户进度',
          load: (o) => {
            o.select.options = CustomerMgr.status()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    channelSource: {
      field: 'channelSource',
      minWidth: 160,
      header: {
        filter: {
          title: '来源渠道',
          key: 'channelSource',
          placeholder: '请选择来源渠道',
          load: (o) => {
            o.select.options = CustomerMgr.channelSource()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    intentionLevel: {
      field: 'intentionLevel',
      minWidth: 120,
      header: {
        filter: {
          title: '意向等级',
          key: 'intentionLevel',
          placeholder: '请选择意向等级',
          load: (o) => {
            o.select.options = CustomerMgr.intentionLevel()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    intentionPurpose: {
      field: 'intentionPurpose',
      minWidth: 160,
      header: {
        filter: {
          title: '置业目的',
          key: 'intentionPurpose',
          placeholder: '请选择置业目的',
          load: (o) => {
            o.select.options = CustomerMgr.intentionPurpose()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    intentionProduct: {
      field: 'intentionProduct',
      minWidth: 120,
      title: '意向业态',
      header: {
        filter: {
          title: '意向业态',
          key: 'intentionProduct',
          placeholder: '请选择意向业态',
          load: (o) => {
            o.select.options = CustomerMgr.intentionProduct()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },

    intentionHouseType: {
      field: 'intentionHouseType',
      minWidth: 160,
      header: {
        filter: {
          title: '意向户型',
          key: 'intentionHouseType',
          placeholder: '请选择意向户型',
          load: (o) => {
            o.select.options = CustomerMgr.intentionHouseType()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    intentionArea: {
      field: 'intentionArea',
      minWidth: 160,
      header: {
        filter: {
          title: '意向面积',
          key: 'intentionArea',
          placeholder: '请选择意向面积',
          load: (o) => {
            o.select.options = CustomerMgr.intentionArea()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    intentionTotalPrice: {
      field: 'intentionTotalPrice',
      minWidth: 160,
      header: {
        filter: {
          title: '意向总价',
          key: 'intentionTotalPrice',
          placeholder: '请选择意向总价',
          load: (o) => {
            o.select.options = CustomerMgr.intentionTotalPrice()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    intentionAveragePrice: {
      field: 'intentionAveragePrice',
      minWidth: 160,
      header: {
        filter: {
          title: '意向单价',
          key: 'intentionAveragePrice',
          placeholder: '请选择意向单价',
          load: (o) => {
            o.select.options = CustomerMgr.intentionAveragePrice()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    intentionRegion: {
      field: 'intentionRegion',
      minWidth: 140,
      header: {
        filter: {
          title: '意向区域',
          key: 'intentionRegion',
          placeholder: '请选择意向区域',
          load: (o) => {
            o.select.options = CustomerMgr.intentionRegion()
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    //写死两个
    sourceType: {
      field: 'sourceType',
      minWidth: 120,
      header: {
        filter: {
          title: '创建方式',
          key: 'createMethod',
          placeholder: '请选择创建方式',
          select: {
            options: [
              { key: 1, value: '扫码授权' },
              { key: 2, value: '手动录入' },
              { key: 3, value: '网络来客' },
              { key: 5, value: '码上获客' }
            ],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    checkInMethod: {
      field: 'checkInMethod',
      minWidth: 120,
      header: {
        filter: {
          title: '签到方式',
          key: 'checkInMethod',
          placeholder: '请选择签到方式',
          select: {
            options: [
              { key: 1, value: '扫码签到' },
              { key: 2, value: '手动签到' },
              { key: 3, value: '新增签到' }
            ],
            multiple: true,
            filterable: true
          }
        }
      }
    },

    // 三个时间
    createTime: {
      field: 'createTime',
      minWidth: 160,
      header: {
        filter: {
          title: '创建日期',
          key: 'customerCreateStartTime',
          placeholder: '请选择创建日期',
          date: {
            type: 'daterange'
          },
          data: {
            from: (info) => {
              if (info.customerCreateStartTime && info.customerCreateEndTime) {
                return [info.customerCreateStartTime, info.customerCreateEndTime]
              }
              return null
            },
            to: (v) => {
              return {
                customerCreateStartTime: v[0],
                customerCreateEndTime: v[1]
              }
            }
          }
        }
      },
      cell: {
        value: (row) => (row.createTime ? vm.$tool.dateFtt('yyyy/MM/dd hh:mm', row.createTime) : ''),
        text: {}
      }
    },
    lastVisitTime: {
      field: 'lastVisitTime',
      minWidth: 160,
      header: {
        filter: {
          title: '最近到访时间',
          key: 'lastVisitStartTime',
          placeholder: '请选择最近到访时间',
          date: {
            type: 'daterange'
          },
          data: {
            from: (info) => {
              if (info.lastVisitStartTime && info.lastVisitEndTime) {
                return [info.lastVisitStartTime, info.lastVisitEndTime]
              }
              return null
            },
            to: (v) => {
              return {
                lastVisitStartTime: v[0],
                lastVisitEndTime: v[1]
              }
            }
          }
        }
      },
      cell: {
        value: (row) => (row.lastVisitTime ? vm.$tool.dateFtt('yyyy/MM/dd hh:mm', row.lastVisitTime) : ''),
        text: {}
      }
    },
    lastFollowUpTime: {
      field: 'lastFollowUpTime',
      minWidth: 160,
      header: {
        filter: {
          title: '最近跟进时间',
          key: 'lastFollowUpStartTime',
          placeholder: '请选择最近跟进时间',
          date: {
            type: 'daterange'
          },
          data: {
            from: (info) => {
              if (info.lastFollowUpStartTime && info.lastFollowUpEndTime) {
                return [info.lastFollowUpStartTime, info.lastFollowUpEndTime]
              }
              return null
            },
            to: (v) => {
              return {
                lastFollowUpStartTime: v[0],
                lastFollowUpEndTime: v[1]
              }
            }
          }
        }
      },
      cell: {
        value: (row) =>
          row.lastFollowUpTime ? vm.$tool.dateFtt('yyyy/MM/dd hh:mm', row.lastFollowUpTime) : '',
        text: {}
      }
    },

    gender: {
      field: 'gender',
      title: '性别',
      minWidth: 80
    },
    standbyPhone: {
      field: 'standbyPhone',
      title: '备用号码',
      minWidth: 160
    },
    wechat: {
      field: 'wechat',
      title: '微信号',
      minWidth: 160
    },
    telChangeVisit: {
      field: 'telChangeVisit',
      title: '是否电转访',
      minWidth: 110
    },
    intentionSubway: {
      field: 'intentionSubway',
      title: '意向地铁',
      minWidth: 160
    },
    intentionLoopLine: {
      field: 'intentionLoopLine',
      title: '意向环线',
      minWidth: 140
    },
    downPaymentInterval: {
      title: '首付区间',
      field: 'downPaymentInterval',
      minWidth: 120
    },
    paymentRatio: {
      title: '付款比例',
      field: 'paymentRatio',
      minWidth: 110
    },
    attentionBuilding: {
      title: '关注楼栋',
      field: 'attentionBuilding',
      minWidth: 110
    },
    attentionFloor: {
      title: '关注楼层',
      field: 'attentionFloor',
      minWidth: 120
    },
    attentionFactor: {
      title: '关注因素',
      field: 'attentionFactor',
      minWidth: 160
    },
    resistancePoints: {
      title: '抗性因素',
      field: 'resistancePoints',
      minWidth: 120
    },
    numberVisiting: {
      title: '来访人数',
      field: 'numberVisiting',
      minWidth: 120
    },
    visitorStructure: {
      title: '来访人员结构',
      field: 'visitorStructure',
      minWidth: 120
    },
    hisFirstTime: {
      title: '首访时间',
      field: 'hisFirstTime',
      minWidth: 160,
      cell: {
        value: (row) => (row.hisFirstTime ? vm.$tool.dateFtt('yyyy/MM/dd hh:mm', row.hisFirstTime) : ''),
        text: {}
      }
    },
    receptionTime: {
      title: '接待时长',
      field: 'receptionTime',
      minWidth: 120
    },
    ageRange: {
      title: '客户年龄',
      field: 'ageRange',
      minWidth: 120
    },
    purchaseIntegral: {
      title: '购房积分',
      field: 'purchaseIntegral',
      minWidth: 120
    },
    paymentType: {
      title: '付款方式',
      field: 'paymentType',
      minWidth: 120
      // cell: {
      //   value: (row) => { return dt.dictInfo().__value('paymentType', row.paymentType) },
      //   text: {}
      // }
    },
    historyMarriage: {
      title: '近期婚史',
      field: 'historyMarriage',
      minWidth: 120
    },
    census: {
      title: '户籍',
      field: 'census',
      minWidth: 120
    },
    buyHouseQualification: {
      title: '购房资质',
      field: 'buyHouseQualification',
      minWidth: 120
    },
    loansQualification: {
      title: '贷款资质',
      field: 'loansQualification',
      minWidth: 120
    },
    familyStructure: {
      title: '家庭结构',
      field: 'familyStructure',
      minWidth: 120
    },
    livingArea: {
      title: '居住区域',
      field: 'livingArea',
      minWidth: 120
    },
    workingArea: {
      title: '工作区域',
      field: 'workingArea',
      minWidth: 120
    },
    vehicle: {
      title: '交通工具',
      field: 'vehicle',
      minWidth: 120
    },
    industry: {
      title: '从事行业',
      field: 'industry',
      minWidth: 120
    },
    position: {
      field: 'position',
      title: '职务职位',
      minWidth: 120
    },
    educationBackground: {
      field: 'educationBackground',
      title: '学历',
      minWidth: 120
    },
    capitalSource: {
      field: 'capitalSource',
      title: '资金来源',
      minWidth: 120
    },
    keyDecisionMaker: {
      field: 'keyDecisionMaker',
      title: '决策关键人',
      minWidth: 120
    },
    houseNum: {
      field: 'houseNum',
      title: '现有房产套数',
      minWidth: 140
    },
    customerFeature: {
      title: '客户特征',
      field: 'customerFeature',
      minWidth: 200
    },
    workUnit: {
      title: '工作单位',
      field: 'workUnit',
      minWidth: 110
    },
    workUnit: {
      title: '工作单位',
      field: 'workUnit',
      minWidth: 110
    },
    annualIncome: {
      title: '年收入',
      field: 'annualIncome',
      minWidth: 110
    },
    nowLiveHouse: {
      title: '居住小区',
      field: 'nowLiveHouse',
      minWidth: 110
    },
    homeCount: {
      title: '置业次数',
      field: 'homeCount',
      minWidth: 110
    },
    visitCount: {
      title: '来访次数',
      field: 'visitCount',
      minWidth: 110
    },
    demandLand: {
      title: '需求地段',
      field: 'demandLand',
      minWidth: 110
    },
    cognitiveChannel: {
      title: '认知途径',
      field: 'cognitiveChannel',
      minWidth: 110
    },
    competitorArea: {
      title: '竞品区域',
      field: 'competitorArea',
      minWidth: 110
    },
    competitorCommunity: {
      title: '竞品小区',
      field: 'competitorCommunity',
      minWidth: 110
    },
    homeOwnershipPlanTime: {
      title: '计划置业时间',
      field: 'homeOwnershipPlanTime',
      minWidth: 110
    },
    isCapitalVerification: {
      title: '是否验资',
      field: 'isCapitalVerification',
      minWidth: 110
    },
    isUpgradeCapitalVerification: {
      title: '是否升级验资',
      field: 'isUpgradeCapitalVerification',
      minWidth: 110
    },

    featurePhoto: {
      // title: '附件特征',
      // field: 'featurePhoto',
      // minWidth: 110,

      field: 'featurePhoto',
      minWidth: 140,
      title: '特征附件',
      slots: {
        default: (params, h) => {
          let img = {
            event: 'lookImg',
            src: params.row.featurePhoto ? params.row.featurePhoto[0] : '',
            data: params.row.featurePhoto
          }
          // return table.cell.img(_this, h, img, true)
          return params.row.featurePhoto ? table.cell.img(_this, h, img, true) : '-'
        }
      }
    }
  }

  console.log(Object.keys(columns2).length, 'columns2columns2columns2111length11')
  return columns2
}

function columns(head, _this) {
  let fields = columnField(_this),
    data = []
  for (let item of head) {
    data.push(fields[item.field] || item)
  }
  return data
}

function options(_this, head) {
  vm = _this
  return [
    ...columns(head, _this),
    {
      field: 'handles',
      title: '操作',
      width: 150,
      fixed: 'right',
      align: 'center',
      className: 'conciseContent',
      slots: {
        default: (params, h) => {
          return [
            h(
              'div',
              {
                style: {
                  display: 'flex',
                  flexDirection: 'column'
                }
              },
              [
                h(
                  'Button',
                  {
                    props: {
                      type: 'text'
                    },
                    style: {
                      cursor: 'pointer',
                      padding: '0 10px'
                    },
                    on: {
                      click: (e) => {
                        e.stopPropagation()
                        vm.viewClick(params.row)
                      }
                    }
                  },
                  '查看详情'
                ),

                h(
                  'Poptip',
                  {
                    props: {
                      transfer: true,
                      width: '380',
                      trigger: 'click',
                      confirm: true,
                      okText: vm.popLen == 0 ? '' : `查看全部${vm.popLen}条记录`,
                      cancelText: '关闭',
                      popperClass: 'M_poptip' + ' ' + vm.popClass
                    },

                    on: {
                      'on-ok': () => {
                        vm.viewClick(params.row, true)
                      }
                    }
                  },
                  [
                    h(
                      'Button',
                      {
                        props: {
                          type: 'text'
                        },
                        style: {
                          color: params.row.newNews == '1' ? '#ed4014' : '#0F42A7',
                          cursor: 'pointer',
                          padding: '0 10px'
                        },
                        on: {
                          click: (e) => {
                            vm.getFollowUp(params.row)
                          }
                        }
                      },
                      params.row.newNews == '1'
                        ? '有新的跟进'
                        : `跟进(${params.row.followNumber})&批注(${params.row.annotationNumber})`
                    ),
                    h(poptipContent, {
                      slot: 'title',
                      props: {
                        followUpList: vm.followUpList,
                        popData: params.row,
                        popLoading: vm.popLoading,
                        intentionLevelFlag: true
                      },

                      on: {
                        popClose: (val) => {
                          vm.popClass = 'popClass'
                        },
                        initList: (val) => {
                          vm.initData()
                        }
                      }
                    })
                  ]
                )
              ]
            )
          ]
        }
      }
    }
  ]
}

function importColumns() {
  return [
    {
      title: '文件名称',
      key: 'formName',
      minWidth: 100
    },
    {
      title: '导入状态',
      key: 'status',
      minWidth: 100,

      render: (h, params) => {
        return h(
          'div',
          {
            style: {
              color: params.row.status == 0 ? '#ed4014' : '#515a6e'
            }
          },
          vm.$formatter.switchName('importStatus', params.row.status)
        )
      }
    },
    {
      title: '导入时间',
      key: 'updateTime',
      minWidth: 160,
      render: (h, params) => {
        return h(
          'div',
          params.row.updateTime && params.row.updateTime != '-'
            ? vm.$tool.dateFtt('yyyy/MM/dd hh:mm', params.row.updateTime)
            : '-'
        )
      }
    },
    {
      title: '操作',
      key: 'result',
      minWidth: 80,
      render: (h, params) => {
        return h('div', [
          h(
            'span',
            {
              props: {
                type: 'text'
              },
              style: {
                color: '#2d8cf0',
                cursor: 'pointer'
              },
              on: {
                click: (e) => {
                  e.stopPropagation()
                  vm.importDownload(params.row.id)
                }
              }
            },
            params.row.status == 1 ? '下载' : ''
          )
        ])
      }
    }
  ]
}
export default {
  importColumns,
  options
}
